package com.yijianonghu.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DBUtils {
	
	private static final Logger logger=LoggerFactory.getLogger(DBUtils.class);
	
	public static boolean isEqualInteger(Integer value1, Object object2){
		if(value1==null || object2==null){
			return false;
		}
		Integer value2=null;
		try {
			value2=parseToNumber(Integer.class, object2);
			return (value1-value2)==0;
		} catch (Exception e) {
			logger.error("比较Integer失败！Integer: "+value1+", Object: "+object2);
		}
		return false;
	}
	
	public static <E extends Number> E parseToNumber(Class<? extends E> clazz, Object object) throws Exception{
		E result=null;
		try {
			Method method=clazz.getMethod("valueOf", String.class);
			result=(E) method.invoke(null, String.valueOf(object));
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			logger.error("将对象解析成数字失败！对象:"+object+"， 数字类:"+clazz,e);
			throw e;
		} catch(Exception e){
			logger.error("异常：将对象解析成数字失败！对象:"+object+"， 数字类:"+clazz,e);
			throw e;
		}
		return result;
	}
	
	public static String parseToString(Object object){
		return String.valueOf(object);
	}
	
	public static boolean isListNotEmpty(List list){
		return (list!=null && (!list.isEmpty()));
	}
	
	public static boolean isFirstItemOfListNotEmpty(List list){
		return isListNotEmpty(list)&&(list.get(0)!=null);
	}
}
